{
 "cells": [
  {
   "cell_type": "markdown",
   "source": [
    "Chapter 05\n",
    "# 有关字典的常见操作\n",
    "Book_1《编程不难》 | 鸢尾花书：从加减乘除到机器学习"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "outputs": [],
   "source": [
    "# 使用大括号创建字典\n",
    "person = {'name': 'James', 'age': 88, 'gender': 'male'}"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-07-02T16:38:33.338139Z",
     "end_time": "2024-07-02T16:38:33.343581Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "outputs": [],
   "source": [
    "# 使用 dict() 函数创建字典\n",
    "fruits = dict(apple=88, banana=888, cherry=8888)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-07-02T16:38:37.432865Z",
     "end_time": "2024-07-02T16:38:37.442347Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "James\n",
      "8888\n"
     ]
    }
   ],
   "source": [
    "# 访问字典中的值\n",
    "print(person['name'])\n",
    "print(fruits['cherry'])"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-07-02T16:38:45.149371Z",
     "end_time": "2024-07-02T16:38:45.158152Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'name': 'James', 'age': 28, 'gender': 'male'}\n"
     ]
    }
   ],
   "source": [
    "# 修改字典中的值\n",
    "person['age'] = 28\n",
    "print(person)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-07-02T16:38:55.582172Z",
     "end_time": "2024-07-02T16:38:55.590677Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'name': 'James', 'age': 28, 'gender': 'male', 'city': 'Toronto'}\n"
     ]
    }
   ],
   "source": [
    "# 添加键值对\n",
    "person['city'] = 'Toronto'\n",
    "print(person)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-07-02T16:39:06.466727Z",
     "end_time": "2024-07-02T16:39:06.476250Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'name': 'James', 'age': 28, 'city': 'Toronto'}\n"
     ]
    }
   ],
   "source": [
    "# 删除键值对\n",
    "del person['gender']\n",
    "print(person)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-07-02T16:39:14.252091Z",
     "end_time": "2024-07-02T16:39:14.260836Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "dict_keys(['name', 'age', 'city'])\n",
      "dict_values(['James', 28, 'Toronto'])\n",
      "dict_items([('name', 'James'), ('age', 28), ('city', 'Toronto')])\n"
     ]
    }
   ],
   "source": [
    "# 获取键、值、键值对列表\n",
    "print(person.keys())\n",
    "print(person.values())\n",
    "print(person.items())"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-07-02T16:39:23.687444Z",
     "end_time": "2024-07-02T16:39:23.697070Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [],
   "metadata": {
    "collapsed": false
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
